#include "objects.h"
#include "iostream"
#include "imageLoader.h"
#include "stdlib.h"
#include <ctime>
#include <Math.h>
#include <fstream>
using namespace std;

void renderNumber(int number,float x, float y,float z);
void output(float x, float y,float z, char *string);
void Display(void);
void specialKeyPressed(int key, int x, int y);
void keyboard(unsigned char key, int x, int y);
void goCrazy(int x);
void goCrazy3(int x);
void goCrazy2(int x);
GLuint loadTexture(Image *x);
void drawGift(float x,float y);
void drawBlocks();
void animateBlocks(int x);
void giftTimer(int x);
void displayScore(int score, int x, int y, int z);
/*****************************************************************************************************/
GLfloat scale = 0.0085, scale2 = 0.014,ambR = 0.3,ambG = 0.3, ambB = 0.3;
int giftsTillNow = 0;
int height = 600,whichLight = 1;
bool startRecording,pause = false,pause2 = false,rightDir=false,leftt = true,upp = false,downn = false;
//This is the width of your scene and window
int width  = 1000;
int blocksSize = 20;
Point *myBlocks = new Point[blocksSize];
int random_integerX = 19,random_integerY = 19;
bool changeTheGift = false,whichCamera = false,hobaz = true;
//GLdouble x = 300, y = 5, z = 50;
static char whereToGo = 'a';
static double angleBlocks = 0;
//This is the ratio of width/height
GLdouble aspect_ratio = WIDTH / HEIGHT;
// This is the texture save array of the background board
GLuint _textureId;
// This is the texture save array of the gift
GLuint _textureId2;
// This is the texture save array of the block
GLuint _textureId3;
/*
*This specifies the position of the reference point, which is used in the
*camera (gluLookAt). Here the reference point is the same as the origin.
*/
Point center = Point(0.0f, 0.0f, 0.0f);
Point eye = Point(0.0f, 0.0f, 50.0f);
Point up =  Point(0.0f, 1.0f, 0.0f);

Line lineOfSight = Line(&eye, &center, &up);

GLfloat nangel = 0, uangel = 0, vangel = 0;
GLfloat zt = 0, xt = 0, yt = 0; 
GLfloat zoom = 45;
GLfloat rotationZ = 0;

/*****************************************************************************************************/


Snake s = Snake();
void output(float x, float y,float z,  char *string)
    {
    int len, i;

	glRasterPos3f(x, y,z);
    len = (int) strlen(string);
    for (i = 0; i < len; i++) {
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]);
    }
    }
void renderNumber(int number, float x, float y,float z ){  // number is the score, x and y are the position of the score (as if it's a 2d thing)
		char numstr[10000];
		sprintf_s(numstr, "%d", number);
		output(x, y,z, numstr);
	}
void animateMe(int value)
{
	if(!pause2)
	if(s.move2(whereToGo,random_integerX,random_integerY,myBlocks,&pause2))
	{
			goCrazy3(0);
			giftTimer(1);
			goCrazy(0);
	}
			glutTimerFunc(100,animateMe,0);
			glutPostRedisplay();
	
}
int main(int argc, char** argr) {
	glutInit(&argc,argr);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
	glutInitWindowSize(WIDTH, HEIGHT);
	glutInitWindowPosition(50,50);
	glutCreateWindow("Testing");
	drawBlocks();
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHTING); //Enable lighting
	glEnable(GL_LIGHT0); //Enable light #0
	glEnable(GL_LIGHT1); //Enable light #1
	glEnable(GL_LIGHT2); //Enable light #1
	glEnable(GL_LIGHT3); //Enable light #1
	glEnable(GL_LIGHT4); //Enable light #1
	glEnable(GL_NORMALIZE);
	Image* image = loadBMP("red brick.bmp");
	_textureId = loadTexture(image);
	delete image;

	Image* image2 = loadBMP("gift_box_red.bmp");
	_textureId2 = loadTexture(image2);
	delete image2;
	
	Image* image3 = loadBMP("blocks.bmp");
	_textureId3 = loadTexture(image3);
	delete image3;
	
	giftTimer(1);
	glutDisplayFunc(Display);

	//glutIdleFunc(animate);
	//glutTimerFunc(DELAY, animate, 0);
	glutKeyboardFunc(keyboard); // Tell GLUT to use the method "keyPressed" for key presses  
	glutSpecialFunc(specialKeyPressed);
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glPointSize(9.0);
	glLineWidth(12.0);
	gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
	glutTimerFunc(50,animateMe,0);
	glutTimerFunc(50,goCrazy2,0);
	//glutTimerFunc(1000,giftTimer,1);
	glutMainLoop();
}
void Display(void) {

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	double winHt=-.5;
	
	gluPerspective(45, aspect_ratio, 0.1, 100);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	/************************************************************************************************/
	
	if(!whichCamera)
	{
		double x =s.bodyPositions[0].x;
		double y =s.bodyPositions[0].y;
		double z =s.bodyPositions[0].z+12;
		double upx = 0;
		double upy = 1;
		double upz = 0;
		
		float rrr = z;
		if(upp)
		{
			
			double myangle = 90;
			x += rrr*cos((3.14*myangle)/180);
			y -= rrr*sin((3.14*myangle)/180);
			gluLookAt(x,y,z, s.bodyPositions  [0].x,	s.bodyPositions[0].y, s.bodyPositions[0].z, 0,0,3);
			//free(&myangle);
		}else if(downn)
		{
			double myangle = 90;
			//x -= rrr*cos((3.14*myangle)/180);
			y += rrr*sin((3.14*myangle)/180);
			gluLookAt(x,y,z, s.bodyPositions  [0].x,	s.bodyPositions[0].y, s.bodyPositions[0].z,0,0,3);
		}else if (leftt)
		{
			double myangle = 90;
			x += rrr*sin((3.14*myangle)/180);
			gluLookAt(x,y,z, s.bodyPositions  [0].x,	s.bodyPositions[0].y, s.bodyPositions[0].z, 0,0, 3);
		}else if (rightDir)
		{
			double myangle = -90;
			x += rrr*sin((3.14*myangle)/180);
			gluLookAt(x,y,z, s.bodyPositions  [0].x,	s.bodyPositions[0].y, s.bodyPositions[0].z, 0,0, 3);
		}
		
	}else
	{	
		gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z);
	}

	/************************************************************************************************/
	//start drawing
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
	GLfloat ambientColor[] = {ambR, ambG, ambB, 1.0f}; //Color (0.2, 0.2, 0.2)
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

	//Add positioned light
	GLfloat lightColor0[] = {0.0f, 1.0f, 0.0f, 1.0f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos0[] = {-19.0f, -19.0f,3.0f, 1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

	GLfloat lightColor1[] = {0.0f, 0.0f, 1.0f, 1.0f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos1[] = {19.0f, 19.0f, 3.0f, 1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
	glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

	GLfloat lightColor2[] = {1.0f, 0.0f, 0.0f, 1.0f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos2[] = {-19.0f, 19.0f, 3.0f,1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor2);
	glLightfv(GL_LIGHT2, GL_POSITION, lightPos2);

	GLfloat lightColor3[] = {1.0f, 0.0f, 1.0f, 1.0f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos3[] = {19.0f, -19.0f, 3.0f, 1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor3);
	glLightfv(GL_LIGHT3, GL_POSITION, lightPos3);

	GLfloat lightColor4[] = {1.0f, 1.0f, 1.0f, 0.2f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos4[] = {0.0f, 0.0f, 30.0f, 1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT4, GL_DIFFUSE, lightColor4);
	glLightfv(GL_LIGHT4, GL_POSITION, lightPos4);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId);

	//Bottom
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);



	glPushMatrix();

	s.draw();
	glPushMatrix();
	glColor3f(1,0,0);
	renderNumber(s.score,s.bodyPositions[0].x,s.bodyPositions[0].y,s.bodyPositions[0].z+2);
	glPopMatrix();
	glPushMatrix();
	glColor3f(1, 0, 0.0);
	glTranslatef(0, 0, 0);
	//glRectf(-20, 20, 20, -20);
	glBegin(GL_QUADS);
	glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(-20,20);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-20,-20);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(20,-20);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(20,20);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId3);

	//Bottom
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glColor3d(1,1,1);
	glPushMatrix();
	glPushMatrix();
	displayScore(0,s.bodyPositions[0].x,s.bodyPositions[0].y,s.bodyPositions[0].z+19);
	glPopMatrix();
	for(int i =0  ; i < blocksSize ;i++)
	{
		//glRectd(myBlocks[i].x,myBlocks[i].y,myBlocks[i].x+1,myBlocks[i].y+1);
		glPushMatrix();
		
		glTranslatef(+myBlocks[i].x+0.5,+myBlocks[i].y+0.5,2);
		glRotatef(angleBlocks,0,0,1);
		glTranslatef(-myBlocks[i].x-0.5,-myBlocks[i].y-0.5,-2);
		angleBlocks += 2;

		glBegin(GL_QUADS);
		glNormal3f(0.0, 0.0f, 1.0f);
		glTexCoord2f(1.0f, 0.0f);	
		glVertex3f(myBlocks[i].x,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,2);
		glEnd();
		
		glBegin(GL_QUADS);
		glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(1.0f, 0.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y,1);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,1);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,1);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,1);
		glEnd();

		glBegin(GL_QUADS);
		glNormal3f(0.0, -1.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,1);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y,1);
		glEnd();

		glBegin(GL_QUADS);
		glNormal3f(0.0, 1.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,2);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,1);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,1);
		glEnd();

		glBegin(GL_QUADS);
		glNormal3f(1.0, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y+1,1);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x,myBlocks[i].y,1);
		glEnd();

		glBegin(GL_QUADS);
			glNormal3f(1.0, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,2);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y+1,1);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(myBlocks[i].x+1,myBlocks[i].y,1);
		glEnd();
		//glPushMatrix();
		glPopMatrix();
	}
	glPopMatrix();

	//glRectd(1,1,0,0);
	glPopMatrix();
	glPushMatrix();
	drawGift(random_integerX,random_integerY);
	glBegin(GL_LINES);
	glVertex3f(s.bodyPositions[0].x,s.bodyPositions[0].y,1);
	glVertex3f((float)random_integerX,(float)random_integerY,1);
	glEnd();
	glPopMatrix();
	glPopMatrix();
	glutSwapBuffers();
	glFlush();
}
void keyboard(unsigned char key, int x, int y) {
	switch(key) {
		/*case 'w': if(whereToGo != 's'){whereToGo = 'w';s.move2(key,random_integerX,random_integerY,myBlocks);}
		break;
	case 's': if(whereToGo != 'w'){whereToGo = 's';s.move2(key,random_integerX,random_integerY,myBlocks);}
		break;
	case 'a': if(whereToGo != 'd'){whereToGo = 'a';s.move2(key,random_integerX,random_integerY,myBlocks);}
		break;
	case 'd': if(whereToGo != 'a'){whereToGo = 'd';s.move2(key,random_integerX,random_integerY,myBlocks);}
		break;*/
	case 'a' :
		if(!whichCamera){
			if(leftt)
		{
			s.move2('s',random_integerX,random_integerY,myBlocks,&pause2);
			whereToGo = 's';
			downn = true;
			leftt = false;
		}else if(rightDir)
		{
			s.move2('w',random_integerX,random_integerY,myBlocks,&pause2);
			upp = true;
			whereToGo = 'w';
			rightDir = false;
		}else if(upp)
		{
			s.move2('a',random_integerX,random_integerY,myBlocks,&pause2);
			upp = false;
			whereToGo = 'a';
			leftt = true;

		}else if(downn)
		{
			s.move2('d',random_integerX,random_integerY,myBlocks,&pause2);
			downn = false;
			whereToGo = 'd';
			rightDir = true;
		}
		break;
		}
		else {
			if(upp)
		{
			s.move2('a',random_integerX,random_integerY,myBlocks,&pause2);
			upp = false;
			whereToGo = 'a';
			leftt = true;

		}
			else if(downn)
		{
			s.move2('a',random_integerX,random_integerY,myBlocks,&pause2);
			downn = false;
			whereToGo = 'a';
			leftt = true;

		}
		break;

		}
	case 'd' :
		if(!whichCamera){
			if(rightDir)
		{
			s.move2('s',random_integerX,random_integerY,myBlocks,&pause2);
			downn = true;
			rightDir = false;
			whereToGo = 's';
		}else if(leftt)
		{
			s.move2('w',random_integerX,random_integerY,myBlocks,&pause2);
			upp = true;
			whereToGo = 'w';
			leftt = false;
		}else if(downn)
		{
			s.move2('a',random_integerX,random_integerY,myBlocks,&pause2);
			downn = false;
			whereToGo = 'a';
			leftt = true;

		}else if(upp)
		{
			s.move2('d',random_integerX,random_integerY,myBlocks,&pause2);
			upp = false;
			whereToGo = 'd';
			rightDir = true;
		}
		break;
		}
		else{
			if(upp){
				s.move2('d',random_integerX,random_integerY,myBlocks,&pause2);
			upp = false;
			whereToGo = 'd';
			rightDir = true;

			}
			else if(downn){
				s.move2('d',random_integerX,random_integerY,myBlocks,&pause2);
			downn = false;
			whereToGo = 'd';
			rightDir = true;
		

			}
			break;
		}
	case 'w':{
		if(whichCamera){
			if(leftt){
				s.move2('w',random_integerX,random_integerY,myBlocks,&pause2);
			upp = true;
			whereToGo = 'w';
			leftt = false;


			}
			else if(rightDir){
				s.move2('w',random_integerX,random_integerY,myBlocks,&pause2);
			upp = true;
			whereToGo = 'w';
			rightDir = false;


			}
		}
		break;
			 }
	case 's':
		{
		if(whichCamera){
			if(leftt){
				s.move2('s',random_integerX,random_integerY,myBlocks,&pause2);
			whereToGo = 's';
			downn = true;
			leftt = false;
			}
			else if(rightDir){
				s.move2('s',random_integerX,random_integerY,myBlocks,&pause2);
			downn = true;
			rightDir = false;
			whereToGo = 's';
			}
		}
		break;
		}
		
	case 'p': pause2 = !pause2;
		break;
	case 'c' : whichCamera = !whichCamera;glutPostRedisplay();
		break;
	case 'z' : float rad = sqrt(((eye.x-center.x)*(eye.x-center.x))+((eye.z-center.z)*(eye.z-center.z)));
		rotationZ += 1;
		eye.x =  center.x +  sin((double)((rotationZ*3.14)/180));
		eye.z = center.z  +  cos((double)((rotationZ*3.14)/180));
		glutPostRedisplay();
		break;
	

	}

}
void goCrazy(int l)
{
	if(l<20)
	{
		glDisable(GL_LIGHT4);
		if(l++%2==0)
		{
			ambG = 0.3;
			ambR = 0.3;
			ambB = 0.3;
		}else
		{
			ambG = 1.0;
			ambR = 1.0;
			ambB = 1.0;
		}
		glutTimerFunc(30,goCrazy,l);
	}else
	{
		glEnable(GL_LIGHT4);
		ambG = 0.3;
		ambR = 0.3;
		ambB = 0.3;
	}
	glutPostRedisplay();
}
void goCrazy3(int l)
{
	//if(l<20)
	//{
		glDisable(GL_LIGHT4);
		if(l++%2==0)
		{
			ambG = 0.3;
			ambR = 0.3;
			ambB = 0.3;
		}else
		{
			ambG = 1.0;
			ambR = 1.0;
			ambB = 1.0;
		}
		glutTimerFunc(30,goCrazy,l);
	
	glutPostRedisplay();
}
void goCrazy2(int time)
{
	switch(whichLight++)
	{
	case 1:  glEnable(GL_LIGHT0);glDisable(GL_LIGHT1);glDisable(GL_LIGHT2);glDisable(GL_LIGHT3);break;
	case 2:  glEnable(GL_LIGHT1);glDisable(GL_LIGHT2);glDisable(GL_LIGHT3);break;
	case 3:  glEnable(GL_LIGHT2);glDisable(GL_LIGHT3);break;
	case 4:  glEnable(GL_LIGHT3);glDisable(GL_LIGHT1);glDisable(GL_LIGHT2);glDisable(GL_LIGHT0);whichLight=1;break;
	}
	glutTimerFunc(400,goCrazy2,time);
	glutPostRedisplay();
}
void specialKeyPressed(int key,int x,int y)
{
	int state=glutGetModifiers();
	switch(key)
	{
	case GLUT_KEY_UP:
		{
			if ((state & GLUT_ACTIVE_SHIFT))
			{
				eye.y += 5;
				center.y += 5;
			}else if ((state & GLUT_ACTIVE_ALT))
			{
				float xdir = center.x-eye.x;
				float ydir = center.y-eye.y;
				float zdir = center.z-eye.z;
				float xdirNorm = (xdir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				float ydirNorm = (ydir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				float zdirNorm = (zdir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				// eyex += xdir;
				// centx += xdir;

				//eyey += ydir;
				//centy += ydir;

				eye.z += zdirNorm;
				center.z += zdirNorm;
			}else if ((state & GLUT_ACTIVE_CTRL))
			{
				float angleRot = 5.0f;
				float distance = sqrt(((center.x-eye.x)*(center.x-eye.x))+((center.y-eye.y)*(center.y-eye.y)));
				center.y += (5)*(cos((angleRot)));
			}
		}break;
	case GLUT_KEY_DOWN:
		{
			if ((state & GLUT_ACTIVE_SHIFT))
			{
				eye.y -= 5;
				center.y -= 5;
			}else if ((state & GLUT_ACTIVE_ALT))
			{
				float xdir = center.x-eye.x;
				float ydir = center.y-eye.y;
				float zdir = center.z-eye.z;
				float xdirNorm = (xdir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				float ydirNorm = (ydir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				float zdirNorm = (zdir/sqrt((xdir*xdir)+(ydir*ydir)+(zdir*zdir))); 
				//  eyex -= xdir;
				//	   centx -= xdir;

				//   eyey -= ydir;
				//centy -= ydir;

				eye.z -= zdirNorm;
				center.z -= zdirNorm;
			}else if ((state & GLUT_ACTIVE_CTRL))
			{
				float angleRot = 5.0f;
				float distance = sqrt(((center.x-eye.x)*(center.x-eye.x))+((center.y-eye.y)*(center.y-eye.y)));
				center.y -= (5)*(cos((angleRot)));
			}
		}break;

	case GLUT_KEY_LEFT:
		{
			if ((state & GLUT_ACTIVE_SHIFT))
			{
				eye.x -= 5;
				center.x -= 5;
			}else if ((state & GLUT_ACTIVE_CTRL))
			{
				float angleRot = 5.0f;
				float distance = sqrt(((center.x-eye.x)*(center.x-eye.x))+((center.y-eye.y)*(center.y-eye.y)));
				center.x-= (5)*(cos((angleRot)));
			}


		}break;
	case GLUT_KEY_RIGHT:
		{
			if ((state & GLUT_ACTIVE_SHIFT))
			{
				eye.x += 5;
				center.x += 5;
			}else if ((state & GLUT_ACTIVE_CTRL))
			{
				float angleRot = 5.0f;
				float distance = sqrt(((center.x-eye.x)*(center.x-eye.x))+((center.y-eye.y)*(center.y-eye.y)));
				center.x += (5)*(cos((angleRot)));
			}
		}break;

	}
	glutPostRedisplay();
}
GLuint loadTexture(Image* image) {
	GLuint textureId;
	glGenTextures(1, &textureId); //Make room for our texture
	glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
	//Map the image to the texture
	glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
		0,                            //0 for now
		GL_RGB,                       //Format OpenGL uses for image
		image->width, image->height,  //Width and height
		0,                            //The border of the image
		GL_RGB, //GL_RGB, because pixels are stored in RGB format
		GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
		//as unsigned numbers
		image->pixels);               //The actual pixel data
	return textureId; //Returns the id of the texture
}
void drawGift(float x,float y)
{
	glRasterPos2f(0.0 , 0.0);
	glDisable(GL_TEXTURE_2D);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId2);

	//Bottom
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glPushMatrix();
	glColor3f(1, 1, 1.0);
	glBegin(GL_QUADS);	
	glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(1.0f, 0.0f);	
	glVertex3f(x,y,2);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x+1,y,2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x+1,y+1,2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x,y+1,2);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x,y,1);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x+1,y,1);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x+1,y+1,1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x,y+1,1);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3f(0.0, -1.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x,y,2);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x+1,y,2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x+1,y,1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x,y,1);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3f(0.0, 1.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x,y+1,2);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x+1,y+1,2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x,y+1,1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x+1,y+1,1);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3f(1.0, 0.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x,y,2);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x,y+1,2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x,y+1,1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x,y,1);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3f(1.0, 0.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x+1,y,2);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x+1,y+1,2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x+1,y+1,1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x+1,y,1);
	glEnd();

	/*glTranslatef(x, y, 1);
	glBegin(GL_QUADS);
	glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(-1,1);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-1,-1);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(1,-1);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(1,1);
	glEnd();*/
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
}
void giftTimer(int value)
{
	bool hobaGift = false;
	while(!hobaGift)
	{
		hobaGift = true;
		srand((signed)time(0)); 
		random_integerX = (rand()%20)-10; 
		random_integerY = (rand()%20)-10; 
		for(int i = 0 ; i < blocksSize ;i++)
		{
			if(random_integerX <= myBlocks[i].x+1 && random_integerX >= myBlocks[i].x-1 && random_integerY <= myBlocks[i].y+1 && random_integerY >= myBlocks[i].y-1)
			{
				hobaGift = false;
				break;
			}
		}
	}
	
}
void drawBlocks()
{
	srand((signed)time(0)); 
	for(int i =0  ; i < blocksSize ;i++)
	{
		int random_X = (int)(rand()%40)-30; 
		int random_Y = (int)(rand()%40)-30;
		Point x = Point(random_X,random_Y,1);
		myBlocks[i] = x;
	}
}
void displayScore(int score, int x, int y, int z){
	glPushMatrix();
    glTranslated(0,0,30);
    glScalef(15,15,15);
    glRasterPos3f(0,0,30);
    //glRasterPos2f(0 ,0);
    double size =0;
    int newScore=score;
    while(score>0){
        score = score/10;
        size++;
    }
    
    size--;
    while(size>-1){
        int digit = newScore/(pow(10.0,size));
        newScore = newScore%( (int)pow(10.0,size) );
         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,((int)'0')+digit);
         size--;
    }
    glPopMatrix();
}